package com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.service.impl;

import com.css.fxfzxczh.constants.YNEnum;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.entity.ProLosInfo;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.entity.ReportPublicField;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.repository.ProLosInfoRepository;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.repository.ReportPublicFieldRepository;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.strategy.StrategyService;
import com.css.fxfzxczh.util.PlatformObjectUtils;
import com.css.fxfzxczh.util.UUIDGenerator;
import org.apache.poi.hssf.usermodel.HSSFDataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/02/09
 * @Description: 地震灾害室内（外）财产损失报表
 */
//@Service("地震灾害室内（外）财产损失报表")
@Service("14")
public class ProLosInfoServiceImpl implements StrategyService {

    @Resource
    ProLosInfoRepository proLosInfoRepository;
    @Resource
    ReportPublicFieldRepository reportPublicFieldRepository;

    @Override
    public void reportImport(Workbook workbook, String xlrmId, String previousXlrmId, ReportPublicField reportPublicField) {
        if(!PlatformObjectUtils.isEmpty(previousXlrmId)){
            proLosInfoRepository.deleteByXlrmId(previousXlrmId);
            //删除报表公共字段
            reportPublicFieldRepository.deleteByXlrmId(previousXlrmId);
        }


        ProLosInfo proLosInfo = new ProLosInfo();
        proLosInfo.setXlrmId(xlrmId);
        //获得当前sheet工作表
        Sheet sheet = workbook.getSheetAt(0);
        //获得当前sheet的开始行
        int firstRowNum = sheet.getFirstRowNum();
        //获得当前sheet的结束行
        int lastRowNum = sheet.getLastRowNum();
        //循环除了第一行的所有行
        for (int rowNum = firstRowNum + 1; rowNum <= lastRowNum; rowNum++) {

            String uuid = UUIDGenerator.getUUID();
            proLosInfo.setXpliId(uuid);
            //获得当前行
            Row row = sheet.getRow(rowNum);
            HSSFDataFormatter hSSFDataFormatter = new HSSFDataFormatter();
            if (row == null) {
                continue;
            }
            if (rowNum == 1) {
                proLosInfo.setFillFormUser(hSSFDataFormatter.formatCellValue(row.getCell(1)));

                proLosInfo.setTel(hSSFDataFormatter.formatCellValue(row.getCell(3)));
                proLosInfo.setFillFormTime(hSSFDataFormatter.formatCellValue(row.getCell(5)));
            }
            if (rowNum == 2) {
                proLosInfo.setProvince(hSSFDataFormatter.formatCellValue(row.getCell(1)));
                proLosInfo.setCity(hSSFDataFormatter.formatCellValue(row.getCell(3)));
                proLosInfo.setCounty(hSSFDataFormatter.formatCellValue(row.getCell(5)));
                proLosInfo.setTownship(hSSFDataFormatter.formatCellValue(row.getCell(7)));
                proLosInfo.setVillage(hSSFDataFormatter.formatCellValue(row.getCell(9)));
                reportPublicField.setProvince(proLosInfo.getProvince());
                reportPublicField.setCity(proLosInfo.getCity());
                reportPublicField.setCounty(proLosInfo.getCounty());
                reportPublicField.setTownship(proLosInfo.getTownship());
                //默认删除
                reportPublicField.setDelFlag(YNEnum.Y.toString());
            }
            if (rowNum == 4) {
                proLosInfo.setQuantityTotal(hSSFDataFormatter.formatCellValue(row.getCell(4)));
                proLosInfo.setLossValueTotal(hSSFDataFormatter.formatCellValue(row.getCell(5)));
            }
            if (rowNum > 4) {
                proLosInfo.setIndoorAndOutdoor(hSSFDataFormatter.formatCellValue(row.getCell(1)));
                proLosInfo.setProjectCategory(hSSFDataFormatter.formatCellValue(row.getCell(2)));
                proLosInfo.setMeteringUnit(hSSFDataFormatter.formatCellValue(row.getCell(3)));
                proLosInfo.setUnitPrice(hSSFDataFormatter.formatCellValue(row.getCell(4)));
                proLosInfo.setQuantity(hSSFDataFormatter.formatCellValue(row.getCell(5)));
                proLosInfo.setLossValue(hSSFDataFormatter.formatCellValue(row.getCell(6)));
                proLosInfo.setRemark(hSSFDataFormatter.formatCellValue(row.getCell(7)));
                proLosInfoRepository.save(proLosInfo);
            }
        }
        reportPublicFieldRepository.save(reportPublicField);
    }
}
